package com.course.service.impl;

import com.course.controller.LoginModel;

import com.course.dao.*;

import com.course.model.*;

import com.course.service.*;

import com.course.util.*;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.ui.ModelMap;

import java.io.IOException;
import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service
public class CourseRangeInfoServiceImpl implements CourseRangeInfoService {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
    @Autowired
    CourseRangeInfoMapper courseRangeInfoMapper;
    @Autowired
    TeacherInfoMapper teacherInfoMapper;
    @Autowired
    CourseInfoMapper courseInfoMapper;
    @Autowired
    MajorInfoMapper majorInfoMapper;
    @Autowired
    MajorDirectInfoMapper majorDirectInfoMapper;
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    TermInfoMapper termInfoMapper;
    @Autowired
    StuRangeInfoMapper stuRangeInfoMapper;

    /**
    *根据参数查询课程表列表数据
    */
    @Override
    public Map<String, Object> getDataList(CourseRangeInfo queryModel,
        Integer page, Integer pageSize, LoginModel login) {
        CourseRangeInfoExample se = new CourseRangeInfoExample();
        CourseRangeInfoExample.Criteria sc = se.createCriteria();
        se.setOrderByClause("id desc"); //默认按照id倒序排序

        if ((queryModel.getCourseName() != null) &&
                (queryModel.getCourseName().equals("") == false)) {
            sc.andCourseNameLike("%" + queryModel.getCourseName() + "%"); //模糊查询
        }

        if (queryModel.getTeacherId() != null) {
            sc.andTeacherIdEqualTo(queryModel.getTeacherId());
        }
        if(login.getLoginType()==3){
        	StuRangeInfoExample re = new StuRangeInfoExample();
        	StuRangeInfoExample.Criteria rc =re.createCriteria();
        	rc.andStuIdEqualTo(login.getId());
        	List<StuRangeInfo> rl = stuRangeInfoMapper.selectByExample(re);//查询该学生的所有排课信息
        	List<Integer> rids = new ArrayList<Integer>();//排课ID
        	for(StuRangeInfo r:rl){
        		rids.add(r.getRangeId());
        	}
        	
        	if(rids.size()==0){
        		sc.andIdEqualTo(0);
        	}else{
        		sc.andIdIn(rids);
        	}
        }
        int count = (int) courseRangeInfoMapper.countByExample(se);
        int totalPage = 0;

        if ((page != null) && (pageSize != null)) { //分页

            if ((count > 0) && ((count % pageSize) == 0)) {
                totalPage = count / pageSize;
            } else {
                totalPage = (count / pageSize) + 1;
            }

            se.setPageRows(pageSize);
            se.setStartRow((page - 1) * pageSize);
        }

        List<CourseRangeInfo> list = courseRangeInfoMapper.selectByExample(se); //执行查询语句
        Map<String, Object> rs = new HashMap<String, Object>();
        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();

        for (CourseRangeInfo model : list) {
            list2.add(getCourseRangeInfoModel(model, login));
        }

        rs.put("list", list2); //数据列表
        rs.put("count", count); //数据总数
        rs.put("totalPage", totalPage); //总页数

        return rs;
    }

    /**
      封装课程表为前台展示的数据形式
    */
    @Override
    public Map<String, Object> getCourseRangeInfoModel(CourseRangeInfo model,
        LoginModel login) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("courseRangeInfo", model);

        if (model.getTeacherId() != null) {
            TeacherInfo teacherInfo = teacherInfoMapper.selectByPrimaryKey(model.getTeacherId()); //教师id为外接字段,需要关联教师来解释该字段

            if (teacherInfo != null) {
                map.put("teacherIdStr",
                    teacherInfo.getTno() + "+" + teacherInfo.getRealName());
            }
        }

        if (model.getCourseId() != null) {
            CourseInfo courseInfo = courseInfoMapper.selectByPrimaryKey(model.getCourseId()); //课程id为外接字段,需要关联课程来解释该字段

            if (courseInfo != null) {
                map.put("courseIdStr", courseInfo.getCourseName());
            }
        }

        if (model.getMajorId() != null) {
            MajorInfo majorInfo = majorInfoMapper.selectByPrimaryKey(model.getMajorId()); //专业为外接字段,需要关联专业来解释该字段

            if (majorInfo != null) {
                map.put("majorIdStr", majorInfo.getMajorName());
            }
        }

        if (model.getDirectId() != null) {
            MajorDirectInfo majorDirectInfo = majorDirectInfoMapper.selectByPrimaryKey(model.getDirectId()); //方向为外接字段,需要关联专业方向来解释该字段

            if (majorDirectInfo != null) {
                map.put("directIdStr", majorDirectInfo.getDirectName());
            }
        }

        if (model.getRoomId() != null) {
            RoomInfo roomInfo = roomInfoMapper.selectByPrimaryKey(model.getRoomId()); //教室为外接字段,需要关联教室来解释该字段

            if (roomInfo != null) {
                map.put("roomIdStr", roomInfo.getRoomName());
            }
        }

        map.put("weekDayStr",
            DataListUtils.getWeekDayNameById(model.getWeekDay() + "")); //解释星期几字段
        map.put("jsStr", DataListUtils.getJsNameById(model.getJs() + "")); //解释第几节字段

        if (model.getTermId() != null) {
            TermInfo termInfo = termInfoMapper.selectByPrimaryKey(model.getTermId()); //学期为外接字段,需要关联学期来解释该字段

            if (termInfo != null) {
                map.put("termIdStr", termInfo.getTermName());
            }
        }

        return map;
    }
}

